<?php
class ModelModuleProMegamenu extends Model {
		
	public function index() {

      $this->install();

	}
	public function insert($data) {	
		foreach($data['title'] as $key=>$value)
			$this->db->query("INSERT INTO " . DB_PREFIX . "pro_megamenu SET title = '" . htmlspecialchars_decode($value) . "', url = '" . $data['url'][$key] . "', parent_id = '" . (int)$data['parent_id'][$key] . "', subcontent = '". htmlspecialchars_decode($data['content_submenu'][$key]) ."', type = '".$data['type'][$key]."', type_id = '" .(int)$data['type_id'][$key]. "', widths = '" .$data['widths'][$key]. "', columns = '" .(int)$data['columns'][$key]. "', activemega = '" .(int)$data['activemega'][$key]. "'");
	}
	public function empty_data() {
			$this->db->query("TRUNCATE " . DB_PREFIX . "pro_megamenu");

	}
	public function getMenuHtml($parent_id){
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "pro_megamenu  WHERE " . DB_PREFIX . "pro_megamenu.parent_id = '".$parent_id."' ORDER BY " . DB_PREFIX . "pro_megamenu.id ASC");
		$result=array();
		foreach ($query->rows as $value) {
			$result[] = $value;
		}
		if($result) :
		if($parent_id!=0) echo "<ol class='dd-list'>";
		foreach($result as $value) : ?>
		<li class='dd-item'>
		<div class='dd-handle'>
									<div class='bar'>
										<span class='title'><?php echo $value['title'];?></span>
									</div>
									</div>
									<div class='info hide'>
										<p class="input-item"><span class='type'>Type : <?php echo $value['type'];?></span></p>	
										<p class="input-item"><label>Title : </label>
										<input type='text' name='title[]' value='<?php echo $value['title'];?>'  /></p>
										<?php if($value['type']=='custom') : ?>
										<p class="input-item"><label>Url : </label><input type='text' name='url[]' value='<?php echo $value['url'] ; ?>'/></p>
										<?php endif;?>
										<p class="input-item"><a  href='javascript:void(0);' class='remove' onclick="remove_item(this);">Remove This Menu Item</a></p>
										<?php if($value['activemega']==1) : ?>
											<p class="input-item"><a  href='javascript:void(0);' class='activemega' onclick="activemega(this);" >Deactive Megamenu</a></p>
										<?php else : ?>
											<p class="input-item"><a  href='javascript:void(0);' class='activemega active' onclick="activemega(this);">Active Megamenu</a></p>
										<?php endif;?>	
										<div class='hidden-data'>
											<input type='hidden' class='type' name='type[]' value='<?php echo $value['type'];?>'/>
											<input type='hidden' class='parent_id' name='parent_id[]' value='<?php echo $value['parent_id'];?>'/>
											<input type='hidden' class='type_id' name='type_id[]' value='<?php echo $value['type_id'];?>'/>
											<?php if($value['type']!='custom') : ?>
											<input type='hidden' class='url' name='url[]' value='<?php echo $value['url'] ; ?>'/>
											<?php endif;?>
											<?php if($value['activemega']==1) : ?>
												<input type='hidden' class='activemega' name='activemega[]' value='1'/>
											<?php else : ?>
												<input type='hidden' class='activemega' name='activemega[]' value='0'/>
											<?php endif;?>	
										</div>
										<?php if($value['activemega']==1) : ?>
										<div class='sub-menu-content'>
											<div>
												<p class="input-item"><label>Num of Columns : </label><input name="columns[]" type="text" value="<?php echo $value['columns'] ; ?>"/></p>
												<p class="input-item"><label>Width(Input number only) : </label><input name="widths[]" type="text" value="<?php echo $value['widths'] ; ?>"/></p>
											</div>
											<p><strong>Content of Sub-menu : </strong></p><textarea name='content_submenu[]' style='width: 695px; height: 213px;'><?php echo $value['subcontent'];?></textarea></div>
											<?php else : ?>
											<div class='sub-menu-content' style='display:none'>
											<div>
												<p class="input-item"><label>Num of Columns : </label><input name="columns[]" type="text" value="<?php echo $value['columns'] ; ?>"/></p>
												<p class="input-item"><label>Width(Input number only) : </label><input name="widths[]" type="text" value="<?php echo $value['widths'] ; ?>"/></p>
											</div>
												<p><strong>Content of Sub-menu : </strong></p><textarea name='content_submenu[]' style='width: 695px; height: 213px;'><?php echo $value['subcontent'];?></textarea></div>
												<?php endif;?>
											</div><a href='javascript:void(0);' class='explane' onclick='explane(this)'>Explane</a>
											<?php 
												$this->model_module_pro_megamenu->getMenuHtml($value['id']);
											?>
		</li>
		<?php
		endforeach ;
		if($parent_id!=0) echo "</ol>";
		endif;
	}
	public function getlist() {

      $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "pro_megamenu  WHERE " . DB_PREFIX . "pro_megamenu.parent_id = '0' ORDER BY " . DB_PREFIX . "pro_megamenu.id ASC");
	  $result=array();
	  foreach ($query->rows as $value) {
				$result[] = $value;
	  }	
	  return $result;		
	}
    public function install() {
	$query = $this->db->query("CREATE TABLE IF NOT EXISTS ".DB_PREFIX."pro_megamenu
		(
			id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
			title VARCHAR(225) NOT NULL,
			url VARCHAR(128) NOT NULL,
			parent_id INTEGER(128) NOT NULL DEFAULT 0,
			subcontent TEXT NOT NULL,
			type VARCHAR(128) NOT NULL,
			type_id INTEGER(128) ,
			widths INTEGER,
			columns INTEGER,
			activemega INTEGER
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");

	}
}
?>